package org.bambrikii.examples.rutext.dictionary;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.bambrikii.examples.rutext.lexer.Case;
import org.bambrikii.examples.rutext.lexer.CaseValue;

public abstract class DictionaryLoader<T> {
	public Map<String, T> load(String baseName, String key) {
		ResourceBundle bundle = ResourceBundle.getBundle(baseName);
		String numbers = bundle.getString(key);
		Map<String, T> result = new HashMap<String, T>();
		String[] lines = numbers.split("\n");
		for (String line : lines) {
			String[] kv = line.split(",");
			result.put(kv[0], parseValue(kv[1]));
		}
		return result;
	}

	protected abstract T parseValue(String str);

	public Map<String, CaseValue> loadCases(String baseName, String key) {
		ResourceBundle bundle = ResourceBundle.getBundle(baseName);
		String numbers = bundle.getString(key);
		Map<String, CaseValue> result = new HashMap<String, CaseValue>();
		String[] lines = numbers.split("\n");
		for (String line : lines) {
			String[] kv = line.split(",");
			Case newCase = Case.valueOf(kv[1]);
			CaseValue newCaseValue = new CaseValue(newCase, kv[2]);
			result.put(kv[0], newCaseValue);
		}
		return result;
	}
}
